<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:iftg="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm" method="post" enctype="multipart/form-data">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">消息类型：</label>
                            <div class="col-sm-8">
                                <iftg:select id="name" name="name" dicType="sendNews"
                                             data-placeholder="选择消息类型" class="form-control chosen-select"
                                             tabindex="2" style="width: 100%" defaultValue="1">
                                </iftg:select>
                                <!--<input id="name" class="form-control" type="text">-->
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">文件上传：</label>
                            <div class="col-sm-8">
                                    <input class="form-control" style="width:200px" name="file" id="fileUrl" type="file">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">短信模板：</label>
                            <div class="col-sm-8">
                                <select id="smsDemo" class="form-control">

                                </select>
                            </div>
                        </div>

                        <div id="signBox"></div>
                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
                                <button type="button" id="submit" class="btn btn-primary">发送</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script>
    function isPoneAvailable($poneInput) {

        // var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        // if (!myreg.test($poneInput.val())) {
        //     return false;
        // } else {
        //     return true;
        // }
    }

    $(function () {
        // $("#users").change(function () {
        //     var  phone= $(this).val();
        //     var valid_rule = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;// 手机号码校验规则
        //     if ( ! valid_rule.test(phone)) {
        //         $("#phone").html("手机号码格式有误");
        //         $("#phone").css("display","inline")
        //     }else{
        //         $("#phone").css("display","none")
        //     }
        // });
        var   smsDemo= $("smsDemo").val();

        $.ajax({
            url: "/jzweb/send/messageS",
            type: "post",
            success: function (res) {
                var list = res.data;
                var sendList = '<option value="">-请选择-</option>';
                for (var i = 0; i < list.length; i++) {
                    sendList += '<option value="' + list[i].id + '" data-sign="' + list[i].sign + '">' + list[i].name + '</option>'
                }
                $("#smsDemo").html(sendList);
                console.log("res", res)
            }
        });
        $("#smsDemo").change(function () {
            var that = $(this).val();

            $.ajax({
                url: "/jzweb/send/messageSon",
                data: {id: that},
                type: "post",
                success: function (res) {
                    var sendList = '';
                    var a = res.data.messageContent.split(',');
                    for (var j = 0; j < a.length; j++) {
                        sendList += '<div class="form-group sendB">' +
                            '<label class="col-sm-3 control-label" id="sendKey' + j + '">' + a[j] + '</label>' +
                            '<div class="col-sm-8">' +
                            '<input id="send' + j + '" class="form-control"  />' +
                            '</div></div>'
                    }
                    $("#signBox").html(sendList)
                }
            });
        });
        $("#submit").click(function () {
            //var name = $("#name").val();

            var smsDemo = $("#smsDemo").val();
            var fileUrl = $('#fileUrl');
            var importExcelForm=   $('#signupForm');
            var fileUrls = $('#fileUrl').val();;
            var sms = '';
            $(".sendB").each(function (index, element) {
                var a = $(this).find("#sendKey" + index).html();
                var b = $(this).find("#send" + index).val();
                var c = a + ':' + b;
                if (index == 0) {
                    sms = c;
                } else {
                    sms = sms + ';' + c;
                }
            });
            if(fileUrls.length>0&&fileUrls.length!=0){
                importExcelForm.ajaxSubmit({
                    // $.ajax({
                    url: "/jzweb/send/sendsExl",
                    dataType: "json",
                    data: {
                        // name: name,
                        // data: data,
                        id: smsDemo,
                        messageContent: sms
                    },
                    type: "post",
                    success: function (res) {
                        if(res.code==200){
                            layer.msg("发送成功！");
                        }else {
                            layer.msg(res.msg);
                        }
                    }
                });
            }else {
               // importExcelForm.ajaxSubmit({
                     $.ajax({
                    url: "/jzweb/send/sends",
                    dataType: "json",
                    data: {
                        // name: name,
                        // data: data,
                        id: smsDemo,
                        messageContent: sms
                    },
                    type: "post",
                    success: function (res) {
                        if(res.code==200){
                            layer.msg("发送成功！");
                        }else {
                            layer.msg(res.msg);
                        }
                    }
                });
            }

        })
    })

</script>
</body>
</html>
